import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AgentService } from '@core/routes-services/agent.service';
import { mobileValidate } from '@core/util/formControl-validate-utils';
import { MarketAgentService } from '@core/routes-services/market-agent.service';
import { myValidNull } from '@core/util/string-utils';

@Component({
  selector: 'app--agent-edit',
  templateUrl: './agent-edit.component.html',
})
export class AgentEditComponent implements OnInit {
  entity: any = {};
  marketAgentId: any = null;
  validateForm: FormGroup;
  isLoading: boolean = false;


  districtOption: any [] = [];
  levelOptions: any [] = [];
  allOptions: any [] = [];
  parentsOptions: any [] = [];

  constructor(private fb: FormBuilder,
              private modal: NzModalRef,
              private agentService: AgentService,
              private marketAgentService: MarketAgentService,
              public message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    console.log("------");
    console.log(this.marketAgentId);
    this.initParentsOptions();
    this.validateForm = this.fb.group({
      name: [this.entity.name, [Validators.required, Validators.maxLength(60)]],
      phone: [this.entity.phone, [Validators.required, mobileValidate]],
      idNumber: [this.entity.idNumber, []],
      districtId: [this.entity.districtId, [Validators.required]],
      levelId: [this.entity.levelId, [Validators.required]],
      parentId: [this.entity.parentId, []],
    });
  }



  initParentsOptions() {
    this.isLoading = true;
    this.agentService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.allOptions = res.data.agents;
        // this.filterParentOptions();
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  filterParentOptions() {
    this.validateForm.value.parentId = null;
    let parentLevelId = this.validateForm.value.levelId - 1;
    this.parentsOptions = this.allOptions.filter((t) => t.levelId == parentLevelId);
  }

  submitForm(): void {
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      if(this.validateForm.value.levelId == 4 || this.validateForm.value.levelId == 5){
        // 市级和区级渠道身份证必填
        if(myValidNull(this.validateForm.value.idNumber)){
          this.message.warning("身份证号不能为空");
          return;
        }
      }
      this.isLoading = true;
      if(this.marketAgentId){
        this.marketAgentService.toSignAgent(this.marketAgentId,this.validateForm.value).then((res) => {
          if (res.status.toString().startsWith('20')) {
            this.modal.close(true);
          } else {
            this.message.warning(res.message);
          }
          this.isLoading = false;
        }, error => {
          this.message.error(error.message ? error.message : error.error);
          this.isLoading = false;
        });

      }else {
        this.agentService.saveOrUpdateEntity(this.entity.id, this.validateForm.value).then((res) => {
          if (res.status.toString().startsWith('20')) {
            this.modal.close(true);
          } else {
            this.message.warning(res.message);
          }
          this.isLoading = false;
        }, error => {
          this.message.error(error.message ? error.message : error.error);
          this.isLoading = false;
        });
      }

    }
  }

  close() {
    this.modal.destroy();
  }
}
